#!/usr/bin/env python

import sys

import numpy
import tables
from matplotlib import pyplot
fp = tables.openFile(sys.argv[1])
data = fp.root.data.read()

pedestals = numpy.loadtxt(sys.argv[2])

for i in xrange(0, len(data)-64, 64):
    x = []
    y = []
    ysub = []
    
    start = i
    stop  = i + 64
    for win, samp, adc in data[start:stop]:
        x.append(samp)
        y.append(adc)
        ysub.append(adc - pedestals[win][samp])

    pyplot.subplot(211)
    pyplot.plot(x, y, 'r.', label='Peds not subtracted')

    pyplot.ylabel("ADC counts")
    pyplot.title("Window %d Trig %d Raw" % (win, i/64))

    pyplot.subplot(212)
    pyplot.plot(x, ysub, 'b.', label='Peds subtracted')

    pyplot.xlim(-1.5, 64.5)
    pyplot.xlabel("Sample #")
    pyplot.ylabel("ADC counts")
    pyplot.title("Window %d Trig %d Pedestal Subtracted" % (win, i/64))

    fname = "burst-119MHz-win00-trig{0:04d}.png".format(i/64)
    pyplot.savefig(fname)

    sys.stdout.write("wrote %s\r" % fname)
    sys.stdout.flush()

    pyplot.clf()

print






